<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Ups extends MY_Controller {
	public function __construct(){
		parent::__construct();
		$this->sess_checked();
		$this->up=load_class('CmsUpload');
		$this->img=load_class('Images');
		
		$this->config=getConfig();
		
		$this->setarr=$_POST;
	}
	public function checkCom(){
		$updata=array();
		if(isset($_GET['ctl'])){ $updata['ctl']=$_GET['ctl'];}else{ $updata['ctl']='Ups';}
		if(!isset($_GET['method'])){echo "<h3>不存在上传服务器地址</h3>";exit;}else{$updata['method']=$_GET['method'];}
		if(!isset($_GET['classname'])){echo "<h3>不存在保存数据库classname值</h3>";exit;}else{$updata['classname']=$_GET['classname'];}
		if(!isset($_GET['callback'])){$updata['callback']='uponevalue';}else{$updata['callback']=$_GET['callback'];}
		if(!isset($_GET['buttonname'])){$updata['buttonname']="上传文件";}else{$updata['buttonname']=$_GET['buttonname'];}
		if(!isset($_GET['color'])){$updata['color']=2;}else{$updata['color']=$_GET['color'];}
		return $updata;
	}
	//上传单张图片
	public function upone(){
		$updata=$this->checkCom();
		//上传token验证
		$updata['uploadtoken']=md5(getConfig('uploadtoken'));
		$this->assign('updata',$updata);
		$is_upload_http=getConfig('is_upload_http');
		if($is_upload_http==1){
			$this->display('comdir/uploadone.html');
		}else{
			$this->display('comdir/uploadbase.html');
		}
	}
	//上传图片跨域中间站
	public function upone_domain(){
		$return=urldecode($_GET['data']);
		$str = '<script type="text/javascript">window.parent.upfile('.$return.')</script>';
		echo $str;
	}
	//本地上传处理
	public function baseupone(){
		$updata=$this->checkCom();
		//上传token验证
		$updata['uploadtoken']=md5(getConfig('uploadtoken'));
		$this->assign('updata',$updata);
		$this->display('comdir/uploadbaseone.html');
	}
	//ueditor 单张图片上传跨域处理
	public function upone_ueditor(){
		$return=urldecode($_GET['data']);
		echo $return;
	}

	//当前域名返回上传信息
	public function return_js($return){
		$str = '<script type="text/javascript">window.parent.upfile('.$return.')</script>';
		echo $str;
	}
	//只上传文件返回
	private function up_return($info){
		//返回回调函数
		$return=$this->setarr;
		if(!$info){
			$return['status']=false;
			$return['msg']=$this->up->getError();
		}else{
			$return['status']=true;
			$return['msg']="上传成功";
			$return['imgshow']=$this->config['upload_http'].trim($info[0],'./');
			$return['img']=trim($info[0],'./');
		}
		$return=json_encode($return);
		$this->return_js($return);
	}
	//上传文件和缩略图返回
	private function thumb_return($info){
		//返回回调函数
		$return=$this->setarr;
		if(!$info['thumbimg']){
			$return['status']=false;
			$return['msg']=$this->img->getError();
		}else{
			//是否删除原图
			if($upinfolist['imgdel']==1){unlink($info[0]);}
			$return['status']=true;
			$return['msg']="上传成功";
			$return['imgshow']=$this->config['upload_http'].trim($info['thumbimg'],'./');
			$return['img']=trim($info['thumbimg'],'./');
		}
		$return=json_encode($return);
		$this->return_js($return);
	}
	//上传水印返回
	private function water_return($info){
		//返回回调函数
		$return=$this->setarr;
		if(!$info['waterimg']){
			$return['status']=false;
			$return['msg']=$this->img->getError();
		}else{
			//是否删除原图
			if($upinfolist['imgdel']==1){unlink($info[0]);}
			$return['status']=true;
			$return['msg']="上传成功";
			$return['imgshow']=$this->config['upload_http'].trim($info['waterimg'],'./');
			$return['img']=trim($info['waterimg'],'./');
		}
		$return=json_encode($return);
		$this->return_js($return);
	}
	//上传站点logo
	public function article(){
		$setconfig[]=array('savePath',$this->setarr['classname']);
		$exts=explode(',',$this->config['exts']);
		$setconfig[]=array('exts',$exts);
		$this->up->setconfig($setconfig);
		$files=$_FILES['upfileone'];
		$info=$this->up->upload_one($files);
		if(!$info){$this->up_return($info);}
		if($this->config['thumb']==0 && $this->config['water']==0){//不生产缩略图 和 不生产水印
			$this->up_return($info);
		}elseif($this->config['water']==0 && $this->config['thumb']==1){//生成缩略图 和 不生产水印
			$thumbimg=$this->img->thumb($info[0],$this->config['thumbwidth'],$this->config['thumbheight']);
			$info['thumbimg']=$thumbimg;
			$this->thumb_return($info);
		}elseif($this->config['thumb']==0 && $this->config['water']==1){//生成水印，但是不生产缩略图
			if($this->config['watertype']==0){
				//水印图片处理
				$waterimg='./'.$this->config['waterimg'];
				$info['waterimg']=$this->img->imgWater($info[0],$waterimg,$this->config['watermarkpos']);
			}elseif($this->config['watertype']==1){
				$fonttype=FCPATH.'static/fonts/'.$this->config['waterfont'];
				$info['waterimg']=$this->img->textWater($info[0],$this->config['watertxt'],$this->config['watercolor'],$this->config['watermarkpos'],$this->config['waterfontsize'],$fonttype);
			}
			$this->water_return($info);
		}elseif($this->config['water']==1 && $this->config['thumb']==1){//生成缩略图和 生成水印

			$thumb_img=$this->img->thumb($info[0],$this->config['thumbwidth'],$this->config['thumbheight']);
			if(!$thumb_img){
				$return['msg']=$this->img->getError();
				$resreturn=json_encode($return);
				$this->return_js($resreturn);
				exit;
			}
			if($this->config['watertype']==0){
				//水印图片处理
				$waterimg='./'.$this->config['waterimg'];
				$info['waterimg']=$this->img->imgWater($thumb_img,$waterimg,$this->config['waterMarkpos']);
			}elseif($this->config['watertype']==1){
				$fonttype='./data/fonts/'.$this->config['waterfont'];
				$info['waterimg']=$this->img->textWater($thumb_img,$this->config['watertxt'],$this->config['watercolor'],$this->config['watermarkpos'],$this->config['waterfontsize'],$fonttype);
			}
			$this->water_return($info);
		}
	}
	//上传站点logo
	public function sites(){
		$setconfig[]=array('savePath',$this->setarr['classname']);
		$exts=explode(',',$this->config['exts']);
		$setconfig[]=array('exts',$exts);
		$this->up->setconfig($setconfig);
		$files=$_FILES['upfileone'];
		$info=$this->up->upload_one($files);
		$this->up_return($info);
	}
	//上传水印图片
	public function water(){
		$setconfig[]=array('savePath',$this->setarr['classname']);
		$exts=explode(',',$this->config['exts']);
		$setconfig[]=array('exts',$exts);
		$this->up->setconfig($setconfig);
		$files=$_FILES['upfileone'];
		$info=$this->up->upload_one($files);
		$this->up_return($info);
	}
	//上传栏目图片
	public function column(){
		$setconfig[]=array('savePath',$this->setarr['classname']);
		$exts=explode(',',$this->config['exts']);
		$setconfig[]=array('exts',$exts);
		$this->up->setconfig($setconfig);
		$files=$_FILES['upfileone'];
		$info=$this->up->upload_one($files);
		$this->up_return($info);
	}
	//上传专题图片
	public function special(){
		$setconfig[]=array('savePath',$this->setarr['classname']);
		$exts=explode(',',$this->config['exts']);
		$setconfig[]=array('exts',$exts);
		$this->up->setconfig($setconfig);
		$files=$_FILES['upfileone'];
		$info=$this->up->upload_one($files);
		$this->up_return($info);
	}
	//上传专题背景图片
	public function specialbg(){
		$setconfig[]=array('savePath',$this->setarr['classname']);
		$exts=explode(',',$this->config['exts']);
		$setconfig[]=array('exts',$exts);
		$this->up->setconfig($setconfig);
		$files=$_FILES['upfileone'];
		$info=$this->up->upload_one($files);
		$this->up_return($info);
	}
	//上传管理员图片
	public function adminuserface(){
		$setconfig[]=array('savePath',$this->setarr['classname']);
		$exts=explode(',',$this->config['exts']);
		$setconfig[]=array('exts',$exts);
		$this->up->setconfig($setconfig);
		$files=$_FILES['upfileone'];
		$info=$this->up->upload_one($files);
		$this->up_return($info);
	}
	//上传管理员图片
	public function userinfoface(){
		$setconfig[]=array('savePath',$this->setarr['classname']);
		$exts=explode(',',$this->config['exts']);
		$setconfig[]=array('exts',$exts);
		$this->up->setconfig($setconfig);
		$files=$_FILES['upfileone'];
		$info=$this->up->upload_one($files);
		$this->up_return($info);
	}
	//上传管理员图片
	public function linklogo(){
		$setconfig[]=array('savePath',$this->setarr['classname']);
		$exts=explode(',',$this->config['exts']);
		$setconfig[]=array('exts',$exts);
		$this->up->setconfig($setconfig);
		$files=$_FILES['upfileone'];
		$info=$this->up->upload_one($files);
		$this->up_return($info);
	}
}
